package com.shopmall.pay.service.Impl;

import com.alibaba.fastjson.JSON;
import com.shopmall.feign.order.OrderFeignClient;
import com.shopmall.model.entity.order.OrderInfo;
import com.shopmall.model.entity.order.OrderItem;
import com.shopmall.model.entity.pay.PaymentInfo;
import com.shopmall.pay.mapper.PaymentInfoMapper;
import com.shopmall.pay.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        // 根据订单编号查询支付记录
        PaymentInfo paymentInfo = paymentInfoMapper.getByOrderNo(orderNo);
        // 判断支付记录是否存在
        if (paymentInfo == null) {
            //远程调用获取订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfoOrderNo(orderNo);
            //把orderInfo获取数据封装到paymentInfo里面
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayType(orderInfo.getPayType());
            StringBuilder content = new StringBuilder();
            for (OrderItem item : orderInfo.getOrderItemList()) {
                content.append(item.getSkuName()).append(" ");
            }
            paymentInfo.setContent(content.toString());
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPaymentStatus(0);
            //添加
            paymentInfoMapper.save(paymentInfo);
        }
        return paymentInfo;
    }

    // 支付完成之后，更新状态
    @Override
    public void updatePaymentStatus(Map<String, String> map) {
        // 根据订单编号查询支付记录信息
        PaymentInfo paymentInfo = paymentInfoMapper.getByOrderNo(map.get("out_trade_no"));
        // 判断如果支付记录已经完成，不需要更新
        if (paymentInfo.getPaymentStatus() == 1) {
            return;
        }
        // 没有完成才更新
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOutTradeNo(map.get("trade_no"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(map));
        paymentInfoMapper.updatePaymentInfo(paymentInfo);
        //更新订单状态
        orderFeignClient.updateOrderStatus(paymentInfo.getOrderNo());
//        更新sku商品销量......
    }
}
